package org.jimmy.tang.apache.httpclient;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.log4j.Logger;

public class GatewayHttpClient {

	private static final Logger logger = Logger
			.getLogger(GatewayHttpClient.class);

	/**
	 * 
	 * @param args
	 * @throws IOException
	 * @throws HttpException
	 */
	public static void main(String[] args) throws HttpException, IOException {
		String content = executeGetMethod("http://www.baidu.com");
		logger.debug("\n" + content);
	}

	/**
	 * 
	 * @param backEndURI
	 * @return
	 * @throws IOException
	 * @throws HttpException
	 */
	public static String executeGetMethod(String url) throws HttpException,
			IOException {

		logger.debug("Begin to execute get method.");
		HttpClient client = new HttpClient();

		// Set up HttpClient Get Method
		GetMethod getMethod = new GetMethod(url);

		int statusCode = client.executeMethod(getMethod);
		logger.debug("Status :" + statusCode);
		byte[] responseBody = getMethod.getResponseBody();

		return null == responseBody ? "" : new String(responseBody);
	}
}
